package com.company.cloud.user.modules.mgr.service.impl;

import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.tree.CloudTree;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.StrSplitUtils;
import com.company.cloud.user.constant.UserConstant;
import com.company.cloud.user.modules.api.response.PermissionModel;
import com.company.cloud.user.modules.cache.utils.UserConvertUtils;
import com.company.cloud.user.modules.mgr.controller.req.PermissionSaveParam;
import com.company.cloud.user.modules.mgr.controller.req.PermissionUpdateParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.user.constant.ErrorUserEnum;
import com.company.cloud.user.modules.base.entity.PermissionInfo;
import com.company.cloud.user.modules.base.entity.RolePermissionInfo;
import com.company.cloud.user.modules.base.service.PermissionInfoService;
import com.company.cloud.user.modules.base.service.RolePermissionInfoService;
import com.company.cloud.user.modules.cache.service.UserPermissionCacheService;
import com.company.cloud.user.modules.mgr.service.PermissionMgrService;

import java.util.List;

@Service
public class PermissionMgrServiceImpl implements PermissionMgrService{
	
	@Autowired
	private PermissionInfoService permissionInfoService;
	@Autowired
	private RolePermissionInfoService rolePermissionInfoService;
	@Autowired
	private UserPermissionCacheService userPermissionCacheService;

	@Override
	public List<PermissionModel> tree() {
		List<PermissionInfo> permissionList=permissionInfoService.list();
		CloudTree<PermissionInfo, PermissionModel> tree=new CloudTree<PermissionInfo, PermissionModel>() {
			@Override
			public boolean isTop(PermissionModel entity) {
				return UserConstant.PERMISSION_TOP_CODE.equals(entity.getParentCode());
			}
			@Override
			public boolean isParent(PermissionModel parentObj, PermissionModel childObj) {
				return parentObj.getCode().equals(childObj.getParentCode());
			}
			@Override
			public PermissionModel convert(PermissionInfo entity) {
				return UserConvertUtils.getPermissionModel(entity);
			}
		};
		return tree.tree(permissionList);
	}

	@Override
	public PermissionModel get(String code) {
		PermissionInfo entity=permissionInfoService.getById(code);
		if(entity==null) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		PermissionModel data= UserConvertUtils.getPermissionModel(entity);
		return data;
	}

	@Override
	public PermissionModel save(PermissionSaveParam params, LoginSimpleUserInfo userInfo) {
		ValidatorUtils.validate(params);
		PermissionInfo entity=new PermissionInfo();
		entity.setCode(params.getCode());
		entity.setType(params.getType());
		entity.setName(params.getName());
		entity.setShowName(params.getShowName());
		entity.setIcon(params.getIcon());
		entity.setUrl(params.getUrl());
		entity.setState(params.getState());
		entity.setOrders(params.getOrders());
		entity.setShowPlatform(params.getShowPlatform());
		entity.setRemark(params.getRemark());
		entity.setResource(StrSplitUtils.list2string(params.getResource()));
		entity.setParentCode(params.getParentCode());
		entity.addCreateParam(userInfo);
		entity.addEditParam(userInfo);
		permissionInfoService.save(entity);
		PermissionModel data= UserConvertUtils.getPermissionModel(entity);
		return data;
	}

	@Override
	public PermissionModel update(PermissionUpdateParam params, LoginSimpleUserInfo userInfo) {
		ValidatorUtils.validate(params);
		PermissionInfo entity=permissionInfoService.getById(params.getCode());
		if(entity==null) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		entity.setName(params.getName());
		entity.setShowName(params.getShowName());
		entity.setIcon(params.getIcon());
		entity.setUrl(params.getUrl());
		entity.setState(params.getState());
		entity.setOrders(params.getOrders());
		entity.setShowPlatform(params.getShowPlatform());
		entity.setRemark(params.getRemark());
		entity.setResource(StrSplitUtils.list2string(params.getResource()));
		entity.addEditParam(userInfo);
		permissionInfoService.updateById(entity);
		PermissionModel data= UserConvertUtils.getPermissionModel(entity);
		return data;
	}

	@Transactional
	@Override
	public void delete(String code, LoginSimpleUserInfo userInfo) {
		PermissionInfo entity=permissionInfoService.getById(code);
		if(entity==null) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		int childrenNum= permissionInfoService.count(new QueryWrapper<PermissionInfo>().eq("parent_code", code));
		if( childrenNum > 0 ) {
			throw new BusinessException(ErrorUserEnum.Permission_Has_Children);
		}
		permissionInfoService.removeById(code);
		rolePermissionInfoService.remove(new QueryWrapper<RolePermissionInfo>().eq("per_code", code));
		//todo 检查删除后，缓存问题
	}

	
}
